package cn.smvc.wechat.test.dao.impl;

import java.sql.SQLException;

import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.BeanProcessor;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.RowProcessor;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.springframework.stereotype.Repository;

import cn.smvc.common.dao.JDBCUtils;
import cn.smvc.wechat.test.dao.WeChatAccountDao;
import cn.smvc.wechat.test.entity.WeChatAccount;

@Repository("weChatAccountDao")
public class WeChatAccountDaoImpl implements WeChatAccountDao {
	/*
	 * 这两个是处理数据库表字段下划线的，
	 * 		与实体类中的属性对应（驼峰规则）
	 */
	BeanProcessor bean = new GenerousBeanProcessor();
	RowProcessor processor = new BasicRowProcessor(bean);

	@Override
	public WeChatAccount get(int id) throws SQLException {
		String sql = "select * from wechat_account where id=?";
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		WeChatAccount account = null;
		account = queryRunner.query(sql, new BeanHandler<WeChatAccount>(WeChatAccount.class, processor), id);
		return account;
	}

	@Override
	public WeChatAccount getByToUserName(String toUserName) throws SQLException {
		String sql = "select * from wechat_account a where a.desc=?";
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		WeChatAccount account = null;
		account = queryRunner.query(sql, new BeanHandler<WeChatAccount>(WeChatAccount.class, processor), toUserName);
		return account;
	}

}
